package com.whli.autumn.mall.web.controller.metadata;

import com.github.pagehelper.PageInfo;
import com.whli.autumn.core.app.annotation.ApiVersion;
import com.whli.autumn.core.extend.controller.BaseController;
import com.whli.autumn.core.permission.annotation.RequirePermissions;
import com.whli.autumn.core.share.ResponseResult;
import com.whli.autumn.mall.web.controller.convert.metadata.PerUnitConvert;
import com.whli.autumn.mall.metadata.domain.dto.perunit.*;
import com.whli.autumn.mall.metadata.domain.entity.PerUnit;
import com.whli.autumn.mall.metadata.service.IPerUnitService;
import com.whli.autumn.mall.metadata.domain.vo.perunit.PerUnitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>计量单位API</p>
 *
 * @author whli
 * @version 1.0.0
 * @since 2023-05-22 13:57
 */
@Api(tags = "metadata-计量单位")
@ApiVersion
@RestController
@RequestMapping(value = "/metadata/perUnit")
public class PerUnitController extends BaseController<PerUnit, IPerUnitService> {

    /**
     * <p>查询计量单位详情</p>
     *
     * @author whli
     * @since 2023-05-22 13:57
     * @param id 计量单位主键
     * @return
     */
    @ApiOperation(value = "查询计量单位", httpMethod = "GET")
    @ApiImplicitParam(value = "主键", name = "id", required = true)
    @RequirePermissions("metadata:per-unit:view")
    @GetMapping
    public ResponseResult<PerUnitVO> view(String id) {
        PerUnit entity = service.getByPK(id);
        return ResponseResult.success(PerUnitConvert.INSTANCE.domainToVO(entity));
    }

    /**
     * <p>分页查询计量单位</p>
     *
     * @author  whli
     * @since   2023-05-22 13:57
     * @param   entity 分页参数
     * @return
     */
    @ApiOperation(value = "分页查询计量单位", httpMethod = "GET")
    @RequirePermissions("metadata:per-unit:list")
    @GetMapping("page")
    public ResponseResult<PageInfo<PerUnitVO>> listByPage(PerUnitPageDTO entity) {
        PageInfo<PerUnitVO> page = service.listByPage(entity);
        return ResponseResult.success(page);
    }

    /**
     * <p>查询所有可用计量单位</p>
     *
     * @author  whli
     * @since   2023-05-22 13:57
     * @param   entity 分页参数
     * @return
     */
    @ApiOperation(value = "查询所有可用计量单位", httpMethod = "GET")
    @RequirePermissions("user")
    @GetMapping("list")
    public ResponseResult<List<PerUnitVO>> listByPage(PerUnitQueryDTO entity) {
        List<PerUnitVO> result = service.listPerUnit(entity);
        return ResponseResult.success(result);
    }

    /**
     * <p>新增计量单位</p>
     *
     * @author  whli
     * @since   2023-05-22 13:57
     * @param   entity 新增参数
     * @return
     */
    @ApiOperation(value = "新增计量单位", httpMethod = "POST", consumes = "application/json")
    @RequirePermissions("metadata:per-unit:add")
    @PostMapping
    public ResponseResult<String> save(@RequestBody @Valid PerUnitAddDTO entity) {
        String id = service.save(PerUnitConvert.INSTANCE.addToDomain(entity));
        return !StringUtils.isEmpty(id) ? ResponseResult.success("新增计量单位成功") : ResponseResult.fail("新增计量单位失败");
    }

    /**
     * <p>修改计量单位</p>
     *
     * @author  whli
     * @since   2023-05-22 13:57
     * @param   entity  修改参数
     * @return
     */
    @ApiOperation(value = "修改计量单位", httpMethod = "PUT", consumes = "application/json")
    @RequirePermissions("metadata:per-unit:update")
    @PutMapping
    public ResponseResult<String> update(@RequestBody @Valid PerUnitUpdateDTO entity) {
        String id = service.update(PerUnitConvert.INSTANCE.updateToDomain(entity));
        return !StringUtils.isEmpty(id) ? ResponseResult.success("修改计量单位成功") : ResponseResult.fail("修改计量单位失败");
    }

    /**
     * <p>删除计量单位</p>
     *
     * @author  whli
     * @since   2023-05-22 13:57
     * @param   ids 主键集合 java.util.List<String>
     * @return
     */
    @ApiOperation(value = "删除计量单位", httpMethod = "DELETE")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "主键集合", name = "ids", required = true)
    })
    @RequirePermissions("metadata:per-unit:delete")
    @DeleteMapping
    public ResponseResult<String> delete(@RequestBody List<String> ids) {
        boolean flag = service.deleteBatch(ids);
        return flag ? ResponseResult.success("删除计量单位成功") : ResponseResult.fail("删除计量单位失败");
    }

    /**
     * <p>启用计量单位</p>
     *
     * @author whli
     * @since 2023/5/22 14:11
     * @param entity
     * @return
     */
    @ApiOperation(value = "启用计量单位", httpMethod = "PUT")
    @RequirePermissions({"metadata:per-unit:enable"})
    @PutMapping({"enable"})
    public ResponseResult<String> enable(@RequestBody @Valid PerUnitStatusDTO entity) {
        boolean flag = service.enable(entity.getId());
        return flag ? ResponseResult.success("启用计量单位成功") : ResponseResult.fail("启用计量单位失败");
    }

    /**
     * <p>禁用计量单位</p>
     *
     * @author whli
     * @since 2023/5/22 14:11
     * @param entity
     * @return
     */
    @ApiOperation(value = "禁用计量单位", httpMethod = "PUT")
    @RequirePermissions({"metadata:per-unit:disable"})
    @PutMapping({"disable"})
    public ResponseResult<String> disable(@RequestBody @Valid PerUnitStatusDTO entity) {
        boolean flag = service.disable(entity.getId());
        return flag ? ResponseResult.success("禁用计量单位成功") : ResponseResult.fail("禁用计量单位失败");
    }

}
